package com.jied.duodian.web;

import com.jied.duodian.entity.DdOrder;
import com.jied.duodian.entity.DdProduct;
import com.jied.duodian.entity.DdShopping;
import com.jied.duodian.entity.DdUser;
import com.jied.duodian.service.DdOrderService;
import com.jied.duodian.service.DdProductService;
import com.jied.duodian.service.DdShoppingService;
import com.jied.duodian.service.DdUserService;
import com.jied.duodian.service.Impl.DdShoppingServiceImpl;
import com.jied.duodian.service.Impl.DdUserServiceImpl;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/29
 * WWW https://github.com/j6l
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class orderController {

    @Autowired
    DdOrderService ddOrderService;
    @Autowired
    DdShoppingService ddShoppingService;
    @Autowired
    DdUserService ddUserService;
    @Autowired
    SuId suId;
    @Autowired
    DdProductService ddProduct;


    /**
     * 结算判定  生成订单
     *
     * @param dan
     * @param ddShoppinglist
     * @param req
     * @return
     */
    @PostMapping("/jiesuan/{dan}")
    public CommonResult getjiesuan(
            @PathVariable("dan") Integer dan,
            @RequestBody List<DdShopping> ddShoppinglist,
            HttpServletRequest req) {
        Double num = 0.0;
        for (DdShopping shopping : ddShoppinglist) {
            DdOrder ddOrder = new DdOrder();
            DdProduct product = ddProduct.getProductByid( shopping.getProductId() );
            //销售数量
            Integer productNum = shopping.getProductNum();
            product.setStock( product.getStock() - productNum );
            if (product.getStock() < 0) {
                continue;
            }
            product.setActivity( product.getActivity() + productNum );
            ddOrder.setProductId( shopping.getProductId() );
            ddOrder.setUserName( suId.getuserName( req ) );
            ddOrder.setCreateTime( new Date() );
            ddOrder.setOrderSn( suId.getNewId() );
            ddOrder.setProductNum( productNum );
            double nu = product.getMoney() * productNum;
            num += nu;
            ddOrder.setTotalAmount( nu );
            ddOrder.setXiaAddress( dan );
            //保证事务一致性
            ddOrderService.getsdasd( shopping, ddOrder, product );
            log.info( "{}用户,下单成功{}", suId.getuserName( req ), ddOrder );
        }

        return CommonResult.success( num );
    }


    /**
     * 获取购物车商品价格
     *
     * @param ddShoppinglist
     * @return
     */
    @PostMapping("/getmoney")
    public CommonResult getmoney(
            @RequestBody List<DdShopping> ddShoppinglist) {
        Double num = 0.0;
        for (DdShopping shopping : ddShoppinglist) {
            DdProduct product = ddProduct.getProductByid( shopping.getProductId() );
            //销售数量
            Integer productNum = shopping.getProductNum();
            product.setStock( product.getStock() - productNum );
            if (product.getStock() < 0) {
                continue;
            }
            double nu = product.getMoney() * productNum;
            num += nu;
        }

        return CommonResult.success( num );
    }


    /**
     * 添加购物车商品
     *
     * @param id
     * @param req
     * @return
     */
    @GetMapping("/addshopping/{id}")
    public CommonResult addshopping(
            @PathVariable("id") Integer id,
            HttpServletRequest req) {
        DdShopping shopping = new DdShopping();
        shopping.setProductId( id );
        shopping.setProductNum( 1 );
        shopping.setUserId( ddUserService.getUserByName( suId.getuserName( req ) ).getId() );
        DdProduct Product = ddProduct.getProductByid( id );
        shopping.setProductImg( Product.getImgadd() );
        shopping.setProductName( Product.getName() );
        Integer integer = ddShoppingService.addshopping( shopping );
        return CommonResult.success( integer );
    }

    /**
     * 减少购物车商品的数量
     *
     * @param id
     * @param req
     * @return
     */
    @GetMapping("/jianproduct/{id}")
    public CommonResult jianproduct(
            @PathVariable("id") Integer id,
            HttpServletRequest req) {
        DdShopping shopping = ddShoppingService.getshoppingByid( id );
        Integer integer = ddShoppingService.jianshopping( shopping );
        return CommonResult.success( integer );
    }

    /**
     * 增加购物车内商品的数量
     *
     * @param id
     * @return
     */
    @GetMapping("/addproduct/{id}")
    public CommonResult addproduct(
            @PathVariable("id") Integer id) {
        DdShopping shopping = ddShoppingService.getshoppingByid( id );
        Integer integer = ddShoppingService.addshoppingByid( shopping );
        return CommonResult.success( integer );
    }

    /**
     * 删除购物车商品
     *
     * @param id
     * @param req
     * @return
     */
    @GetMapping("/delproduct/{id}")
    public CommonResult delproduct(
            @PathVariable("id") Integer id,
            HttpServletRequest req) {
        DdShopping shopping = ddShoppingService.getshoppingByid( id );
        Integer integer = ddShoppingService.delshoppding( shopping );
        return CommonResult.success( integer );
    }

    /**
     * 获取购物车数据
     *
     * @param req
     * @return
     */
    @GetMapping("/getshopping")
    public CommonResult getshopping(
            HttpServletRequest req) {
        List<DdShopping> integer = ddShoppingService.getshoppingBuuserid( ddUserService.getUserByName( suId.getuserName( req ) ).getId() );
        return CommonResult.success( integer );
    }

    /**
     * 获取登陆者的订单数据
     *
     * @param req
     * @return
     */
    @GetMapping("/getlist")
    public CommonResult getlist(
            HttpServletRequest req) {
        List<DdOrder> integer = ddOrderService.getallbyusername( suId.getuserName( req ) );
        return CommonResult.success( integer );
    }
}
